<?php

class FDocument {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	public function RewriteLinkToFile($url) {
		$pathname = '';
		$file = '';
		$manage= FIN_categoryBean::get_manage_by_Url($this->db,$url );
		if($manage!=''){
			$para = $manage->url;
			$link = $manage->manage_name;
			if($link == 'page'){	
				$pathname = "Fpage";
				$file = 'Fpage';
			}else if($link == 'view'){	
				$pathname = "view";			
				$file = 'FProduct';
			}
		}else{
			$link = $url;
			if($link =='home' || $url == ''){ 
				$pathname = "home";
				$file = 'FHome';
				$this->smarty->assign("MENUHOME", 'home');		
			}else if($link == 'contact'){	
				$pathname = "contact us";			
				$file = 'FContacts';
			}else if($link == 'sitemap'){	
				$pathname = "contact us";			
				$file = 'FContacts';
			}
		}
		
		//$path = "<a href='".FOLDER_HOST.$link."'>".ntp_text($pathname)."</a>";		
		//$this->smarty->assign("PATHROOT", $path);		
		$this->smarty->assign("MENUACTIVE", $para);		
		return $file;
	}
	
	public function RewriteLinkToName($link) {
		$url = str_replace('-'," ", $link);
		
		return $url;
	}
	
	public function RewriteNameToLink($link) {
		$link = JKDocument::khongdau($link);
		$url = str_replace(' ',"-", $link);
		return $url;
	}
	
	public function RewriteLink($id,$name) {
		$name = JKDocument::khongdau($name);
		$name = str_replace(' ',"-", $name);
		$name = str_replace('/',"", $name);
		$url = $id.'_'.$name;
		return $url;
	}
	
	public function GetIdFromLink($link) {
		$url=explode("_",$link);
		return $url[0];
	}
	
	public function WritePath($path,$page,$cat,$product) {
		$str = '';
		if($page!=''){
			$str .= '<a href="'.$path.$page.'">';
		}	
	}
	
	public function ChangePath($str) {
		$str = str_replace("../".UPLOAD_FILES,FOLDER_HOST.UPLOAD_FILES, $str);
		return $str;
	}
	public function ChangeLink($str) {
		$str = str_replace('href="../','href="'.FOLDER_HOST, $str);
		$str = str_replace('data="../','data="'.FOLDER_HOST, $str);
		$str = str_replace('<param name="data" value="../','<param name="data" value="'.FOLDER_HOST, $str);
		$str = str_replace('<param name="src" value="../','<param name="src" value="'.FOLDER_HOST, $str);
		return $str;
	}
	
	public function ChangeLinkTags($str) {
		
		$arr= explode(",",$str);
		return $arr;
	}
	
	
	public function khongdau($str) {
		$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
		$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
		$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
		$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
		$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
		$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
		$str = preg_replace("/(đ)/", 'd', $str);
		$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
		$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
		$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
		$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
		$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
		$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
		$str = preg_replace("/(Đ)/", 'D', $str);
		$str = preg_replace('/(")/', '', $str);
		$str = preg_replace("/(')/", '', $str);
		//$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
		return $str;
	}	

	public function check_email_address($email) {
    	// First, we check that there's one @ symbol, and that the lengths are right
    	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    	// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    	return false;
    	}
    	// Split it into sections to make life easier
    	$email_array = explode("@", $email);
    	$local_array = explode(".", $email_array[0]);
   		for ($i = 0; $i < sizeof($local_array); $i++) {
   			if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
   			return false;
   			}
   		}
   		if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1]))  // Check if domain is IP. If not, it should be valid domain name
   		{
   			$domain_array = explode(".", $email_array[1]);
   			if (sizeof($domain_array) < 2) 
   			{
   			return false; // Not enough parts to domain
   			}
   			for ($i = 0; $i < sizeof($domain_array); $i++) 
   			{
   				if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) 
   				{
   				return false;
   				}
   			}
   		}
   		return true;
	}
}
?>
